/**
 * 参数与路径拼接
 * @param {string} url = 路径
 * @param {Object} data = {参数}
 */
function builderUrlParams(url, data) {
	if (typeof url == 'undefined' || url == null || url == '') {
		return ''
	}
	if (typeof data == 'undefined' || data == null || typeof data != 'object') {
		return url
	}
	url += url.indexOf('?') != -1 ? '' : '?'
	for (var k in data) {
		// url += (url.indexOf('=') != -1 ? '&' : '') + k + '=' + encodeURI(data[k])
		url += `${url.charAt(url.length-1)=== '?'? '':'&'}${k}=${encodeURI(data[k])}`
	}
	return url
}

export default {
	/**
	 * 保留当前页面，跳转到应用内的某个页面
	 * @param {string} Url ='路径'
	 * @param {object} obj = {参数}
	 */
	navigate: (Url, obj) => {
		let jointUrl = "";
		if (obj) {
			jointUrl = builderUrlParams(Url, obj)
		} else {
			jointUrl = Url;
		}
		if (jointUrl) {
			uni.navigateTo({
				url: jointUrl,
				animationType: 'pop-in',
				animationDuration: 300
			});
		}
	},
	/**
	 * 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
	 * @param {string} Url ='路径'
	 */
	switchTab: (url) => {
		uni.switchTab({
			url
		});
	},
	/**
	 * 关闭当前页面，跳转到应用内的某个页面
	 * @param {string} Url ='路径'
	 * @param {object} obj = {参数}
	 */
	redirect: (Url, obj) => {
		let jointUrl = "";
		if (obj) {
			jointUrl = builderUrlParams(Url, obj)
		} else {
			jointUrl = Url;
		}
		if (jointUrl) {
			uni.redirectTo({
				url: jointUrl,
				animationType: 'pop-in',
				animationDuration: 300
			});
		}
	},
	/**
	 * 关闭所有页面，打开到应用内的某个页面
	 * @param {string} Url ='路径'
	 * @param {object} obj = {参数}
	 */
	reLaunch: (Url, obj) => {
		let jointUrl = "";
		if (obj) {
			jointUrl = builderUrlParams(Url, obj)
		} else {
			jointUrl = Url;
		}
		if (jointUrl) {
			uni.reLaunch({
				url: jointUrl
			});
		}
	},
	/**
	 * 关闭当前页面，返回上一页面或多级页面
	 * @param {number} num = 1
	 */
	back: (num = 1) => {
		if (typeof(num) != 'number') return;
		uni.navigateBack({
			delta: num,
		});
	}
}
